import { defineStore } from 'pinia'
import state from './state'
import storage from '@/utils/storage'
import type { UserInfoType, UserStateType, GenderType } from './state'
import { doGetUserData } from '@/apis/afs'
import avatarDefault from '@/assets/img/header.webp'

export const useUserStore = defineStore('userStore', {
    state: () => state,
    actions: {
        ADD_USER_INFO(val: UserInfoType['info']) {
            this.userInfo.info = val
            storage.set(`userStore`, this.userInfo)
            return val
        },
        PUT_USER_INFO(avatar: string, nickname: string, gender: GenderType) {
            this.userInfo.info.avatar = avatar
            this.userInfo.info.nickname = nickname
            this.userInfo.info.gender = gender
            storage.set(`userStore`, this.userInfo)
        },
        ADD_TOKEN(val: { access_token: string; refresh_token: string }) {
            this.userInfo.token = val.access_token
            this.userInfo.refresh_token = val.refresh_token
            storage.set(`userStore`, this.userInfo)
            if (!val.access_token) {
                this.ADD_USER_INFO({ nickname: '', avatar: avatarDefault, gender: 'UNKNOWN', userId: null })
                return ''
            }
            doGetUserData().then(({ code, data, msg }) => {
                if (code !== 200) {
                    this.ADD_USER_INFO({ nickname: '', avatar: avatarDefault, gender: 'UNKNOWN', userId: null })
                    return
                }
                this.ADD_USER_INFO(data)
            })
            return val.access_token
        },
        DEL_TOKEN() {
            // 删除用户和会员信息
            this.userInfo = {
                info: { avatar: '', gender: 'UNKNOWN', nickname: '', userId: '' },
                token: '',
                refresh_token: '',
            }
            this.member = {
                memberType: '',
                memberBenefit: null,
                memberName: null,
            }
            storage.remove(`userStore`)
            storage.remove(`memberInfo`)
        },
        PUT_AUTH_TYPE(val: boolean) {
            this.needAuth = val
            this.refresh_auth_time = new Date()
        },
        SET_MEMBER_INFO(val: UserStateType['member']) {
            this.member = val
            storage.set(`memberInfo`, val)
        },
    },
    getters: {
        getterUserInfo: (state) => {
            return getUserInfoFn(state)
        },
        getterToken: (state) => state.userInfo.token,
        getterMemberInfo: (state): UserStateType['member'] => {
            if (!state.member) {
                return storage.get(`memberInfo`)
            } else {
                return state.member
            }
        },
    },
})
function getUserInfoFn(state: UserStateType) {
    if (judgeNotEmpty(state.userInfo)) {
        const userInfo = storage.get(`userStore`)
        if (judgeNotEmpty(userInfo)) {
            // 拉起授权弹窗
            console.log('拉起授权弹窗')
            state.needAuth = true
            return {
                info: { avatar: '', gender: '', nickname: '', userId: '' },
                token: '',
                refresh_token: '',
            }
        } else {
            state.userInfo = userInfo
            return userInfo
        }
    } else {
        return state.userInfo
    }
}
/**
 * 判断shopInfo是否为空
 * @param {*} obj
 */
function judgeNotEmpty(userInfo: UserInfoType) {
    if (JSON.stringify(userInfo) === '{}' || JSON.stringify(userInfo) === 'null') return true
    if (Object.keys(userInfo).length === 0) return true
    if (!userInfo.token) return true
    return false
}
